/**
 * 07-08-商品-商品管理 <br/>
 *
 * @author fsl fzh 20151125.
 */
$(function() {


    //初始化列表
    initPaging();
    //绑定点击事件
    bindEvent();
});

function initPaging() {
    //初始化供应信息列表
    businessInfo(1);
    //初始化求购信息列表
    logisticsCompany(1);


    $("#add_supplier").attr("href", PAGE_DATA.REST_API.add_supplier_company_url);

}
/**
 * 在页面初始化时, 绑定事件.<br/>
 *
 * @author fzh 20151130
 */
function bindEvent() {
    // 商家推荐 和 物流公司 标签的切换
    $(".am-btn-group>button").click(function() {
        $(".am-btn-group>button").attr("class", "am-btn am-btn-default am-btn-sm");
        $(this).attr("class", "am-btn am-btn-primary am-btn-sm");
        var getName = $(this).attr("id");
        $(".dn").attr("style", "display:none");
        $("." + getName + "").attr("style", "display:block");
        var getCode = $(this).attr("code");
        if (getCode == "1") {
            $(".add-btn").attr("id", "add_logistics");
            $(".add-btn").html("<span style='width:100px;display:block;height: 16px'>新增物流公司</span>");
            $("#add_logistics").attr("href", PAGE_DATA.REST_API.add_logistics_company_url);
        } else {
            $(".add-btn").attr("id", "add_supplier");
            $(".add-btn").html("<span style='width:100px;display:block;height: 16px'>新增商家推荐</span>");
            $("#add_supplier").attr("href", PAGE_DATA.REST_API.add_supplier_company_url);
        }

    });
    //商家推荐信息的删除
    deleAll();
    //物流公司信息的删除
    deleAllCompany();

    //搜索
    $("#search-btn").click(function() {
        var getTab = $(".am-btn-primary").attr("code");
        if (getTab == "0") {
            businessInfo(1);
        } else {
            logisticsCompany(1);
        }

    });
};
/**
 * 获取商家推荐列表数据..<br/>
 * @param page {String} 当前页
 * @author fsl-FZH-20151129.
 */
function businessInfo(page) {
    var onClickObj = {
        onClick: function() {
            var clickPage = $(this).attr("data-page");
            businessInfo(clickPage);
        }
    };
    var goodsListTemp = _.template($("#supplyListTemp").html());
    var getuserName = $("#supply_name").val() || '';
    $.ajax({
        url: PAGE_DATA.REST_API.get_supplier_list_url,
        type: "get",
        dataType: "json",
        data: {
            username: getuserName,
            page: parseInt(page || 0),
            pageSize: 10,
        },
        success: function(result) {
            var datas = $.extend([], result.data);
            $("#supplier-list-context").html(goodsListTemp(datas));
            // 翻页.
            var defaultPagingObj = {
                total: 0,
                pageSize: 0,
                page: 0
            };

            var pagingConf = $.extend(defaultPagingObj, result.pagingConfData, onClickObj);

            adminUtil.initPaging('#comm-paging-0', pagingConf);


        },

        error: function(result) {
            $("#supplier-list-context").html([]);
        }
    });
}
/**
 * 获取物流公司列表数据..<br/>
 * @param page {String} 当前页
 * @author fsl-FZH-20151130.
 */
function logisticsCompany(page) {
    var onClickObj = {
        onClick: function() {
            var clickPage = $(this).attr("data-page");
            logisticsCompany(clickPage);
        }
    };
    var goodsListTemp = _.template($("#logistics-list-temp").html());

    $.ajax({
        url: PAGE_DATA.REST_API.get_logistics_list_url,
        type: "get",
        dataType: "json",
        data: {
            page: parseInt(page || 0),
            pageSize: 10,
        },
        success: function(result) {
            var datas = $.extend([], result.data);
            $("#logistics-company-context").html(goodsListTemp(datas));
            // 翻页.
            var defaultPagingObj = {
                total: 0,
                pageSize: 0,
                page: 0
            };

            var pagingConf = $.extend(defaultPagingObj, result.pagingConfData, onClickObj);

            adminUtil.initPaging('#comm-paging-1', pagingConf);

        },

        error: function(result) {
            $("#logisticsCompany-context").html([]);
        }
    });
}

/**
 * 删除商家推荐信息. 单条 或 多条 都可.<br/>
 * @author fsl-FZH-20151130.
 */
function deleAll() {
    var tblBody = $("#supplier-list-context");
    var selAllChk = $("#sel-all-chk");
    //每行数据的选中效果
    tblBody.on("click", ".sel-chk", function() {
        var chkObj = $(this).find(":checkbox");
        var chkVal = chkObj.prop("checked");
        chkObj.prop("checked", !chkVal);

        var unChk = tblBody.find(":checkbox:not(:checked)").length;
        selAllChk.prop("checked", (unChk == 0));
    });
    // 默认冒泡事件.
    tblBody.on("click", ":checkbox", function(event) {
        // event.stopPropagation();
        var chkVal = $(this).prop("checked");
        $(this).prop("checked", !chkVal);

    });
    // 删除单条数据
    tblBody.on("click", ".del-btn", function() {
        var that = this;
        var delSingle = function() {
            var updateHref = $(this).attr("href") || "";
            updateState(updateHref, '');
        };

        var conf = {
            title: "来自网页的消息",
            message: " 删除后不可恢复，确定删除吗?",
            ok_func: delSingle,
            ok_context: that,
            ok_param: null
        }
        adminUtil.showConfDialog(conf);
    });
    // 全选
    selAllChk.on("click", function() {
        var chkAllVal = $(this).prop("checked");
        tblBody.find(":checkbox").prop("checked", chkAllVal);

    });
    $("#del-supply-btn").on("click", function() {

        var that = this;

        var delIdAry = [];
        tblBody.find(":checked").each(function() {
            delIdAry.push($(this).attr("id"));
        });
        if (delIdAry.length == 0) {

            var conf = {
                title: "提示",
                message: "请选择操作的数据!",
                ok_txt: "确定"
            };
            adminUtil.showMsg(conf);

            return false;
        }
        var delmiult = function() {
            var delIds = delIdAry;
            var deleAllUrl = PAGE_DATA.REST_API.del_supplier_list_url + "?page=" +
                $("#comm-paging-0").attr("current-page") + "&pageSize=10";
            updateState(deleAllUrl, delIds);
        };

        var conf = {
            title: "来自网页的消息",
            message: "删除后不可恢复，确定删除吗?",
            ok_func: delmiult,
            ok_context: that,
            ok_param: null
        };
        adminUtil.showConfDialog(conf);



    });

}
/**
 * 删除 所有物流公司信息. 单条 或 多条 都可.<br/>
 * @author fsl-FZH-20151130.
 */
function deleAllCompany() {
    var tblBody = $("#logistics-company-context");
    var selAllChk = $("#logistics-all-chk");
    tblBody.on("click", ".sel-chk", function() {
        var chkObj = $(this).find(":checkbox");
        var chkVal = chkObj.prop("checked");
        chkObj.prop("checked", !chkVal);

        var unChk = tblBody.find(":checkbox:not(:checked)").length;
        selAllChk.prop("checked", (unChk == 0));
    });
    // 默认冒泡事件.
    tblBody.on("click", ":checkbox", function(event) {
        var chkVal = $(this).prop("checked");
        $(this).prop("checked", !chkVal);

    });
    // 修改单条数据
    tblBody.on("click", ".del-btn", function() {
        var state = $(this).attr("name");
        var that = this;
        var delSingle = function() {
            var updateHref = $(this).attr("href") || "";
            var updateId = $(this).attr("code");
            updateState(updateHref, updateId);
        };

        var conf = {
            title: "来自网页的消息",
            message: " 删除后不可恢复，确定删除吗?",
            ok_func: delSingle,
            ok_context: that,
            ok_param: null
        };
        adminUtil.showConfDialog(conf);
    });
    // 全选
    selAllChk.on("click", function() {
        var chkAllVal = $(this).prop("checked");
        tblBody.find(":checkbox").prop("checked", chkAllVal);

    });
    $("#del-logistics-btn").on("click", function() {

        var that = this;

        var delIdAry = [];
        tblBody.find(":checked").each(function() {
            delIdAry.push($(this).attr("id"));
        });
        if (delIdAry.length == 0) {

            var conf = {
                title: "提示",
                message: "请选择操作的数据!",
                ok_txt: "确定"
            };
            adminUtil.showMsg(conf);

            return false;
        }

        var delmiult = function() {
            var delIds = delIdAry;
            var deleAllUrl = PAGE_DATA.REST_API.del_logistics_list_url + "?page=" +
                $("#comm-paging-1").attr("current-page") + "&pageSize=10";
            updateState(deleAllUrl, delIds);
        };

        var conf = {
            title: "来自网页的消息",
            message: "删除后不可恢复，确定删除吗?",
            ok_func: delmiult,
            ok_context: that,
            ok_param: null
        };
        adminUtil.showConfDialog(conf);



    });

}
/**
 * 删除商品数据. 单条 或 多条 都可.<br/>
 *
 * @param restApi {string} REST API
 * @param companyID {array} companyID
 *
 * @author fsl-FZH-20151130.
 */
function updateState(restApi, companyID) {
    $.ajax({
        url: restApi || '',
        type: "delete",
        data: {
            ids: companyID || '',
            _token: COMM_DATA.csrfToken
        },
        success: function(result) {
            // 重新渲染列表
            var getTab = $(".am-btn-primary").attr("code");
            var getPage = result;
            var getPage1 = result;
            if (getTab == "0") {
                businessInfo(getPage.pagingConfData.page);
            } else {
                logisticsCompany(getPage1.pagingConfData.page);
            }
        },
        error: function(result) {
            // Object {readyState: 4, responseText: "{"data":"\u5bc6\u7801\u9519\u8bef"}", 
            // responseJSON: Object, status: 403, statusText: "Forbidden"}

            //var errorMsg = result.responseJSON.data || '';
            var errorMsg = '';
            var conf = {
                title: "提示",
                message: errorMsg || "获取数据失败!",
                ok_txt: "确定"
            };

            adminUtil.showMsg(conf);
        }

    });

};
